package com.liwang.lanouhn.nvshengo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.liwang.lanouhn.nvshengo.Info.FeatureInfo;
import com.liwang.lanouhn.nvshengo.Info.Info;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.activities.FeatureDetailsActivity;
import com.liwang.lanouhn.nvshengo.view.RecyclerImageView;
import com.squareup.picasso.Picasso;

/**
 * Created by lanouhn on 2016/10/10.
 */
public class FeatureRecyclerViewAdapter extends RecyclerView.Adapter<FeatureRecyclerViewAdapter.FeatureRecyclerViewViewHolder>{

    private Context context;
    private FeatureInfo featureInfo;

    public FeatureRecyclerViewAdapter(Context context, FeatureInfo featureInfo) {
        this.context = context;
        this.featureInfo = featureInfo;
    }

    @Override
    public FeatureRecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_gridview,null);
        FeatureRecyclerViewViewHolder holder=new FeatureRecyclerViewViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(FeatureRecyclerViewViewHolder holder, int position) {
        Picasso.with(context).load(featureInfo.getOverviews().get(position).getCover_url()).fit().config(Bitmap.Config.RGB_565).into(holder.iv_recycleview);
        holder.iv_recycleview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Info info =new Info();
                info.setTitle(featureInfo.getTitle());
                info.setLike_count(featureInfo.getLike_count());
                info.setUrl(featureInfo.getUrl());
                Intent intent =new Intent(context,FeatureDetailsActivity.class);
                intent.putExtra("featureInfo",info);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return featureInfo.getOverviews().size();
    }

    public class FeatureRecyclerViewViewHolder extends RecyclerView.ViewHolder {
        private RecyclerImageView iv_recycleview;
        public FeatureRecyclerViewViewHolder(View itemView) {
            super(itemView);
            iv_recycleview= (RecyclerImageView) itemView.findViewById(R.id.iv_recycleview);
        }
    }

}
